#!/usr/bin/env python
import concurrent.futures
import requests
import traceback
import psycopg2
import time
import json
import re
import sys
from http.cookiejar import LWPCookieJar, Cookie
from bs4 import BeautifulSoup
from hashlib import md5
def loadCookies(path):
    """
    Load cookies from local storage.
    """
    lwp_cookiejar = LWPCookieJar()
    lwp_cookiejar.load(path, ignore_discard=True)
    return lwp_cookiejar


if __name__ == '__main__':
    total_pages = 66
    max_tries = 3
    cookies = loadCookies('/home/atropos/weibo-research/src/python/.cookies/weibo.com')
    url = "http://weibo.com/u/3587960280"
    url = "http://weibo.com/p/1006063587960280?pids=Pl_Official_MyProfileFeed__25&profile_ftype=1&is_all=1&ajaxpagelet=1&ajaxpagelet_v6=1&page=1"
    url = "http://weibo.com/p/aj/v6/mblog/mbloglist?ajwvr=6&domain=100606&profile_ftype=1&is_all=1&pagebar=1&pl_name=Pl_Official_MyProfileFeed__24&id=1006063587960280&feed_type=0&page=1&pre_page=1&domain_op=100606"

    url = "http://weibo.com/p/1005052557582922/follow?pids=Pl_Official_HisRelation__63&from=rel&wvr=5&loc=hisfollow&ajaxpagelet=1&ajaxpagelet_v6=1&page=1"
    url = "http://weibo.com/1678870364/follow"
    headers = {
        "Host": "weibo.com",
        "Connection": "keep-alive",
        "Upgrade-Insecure-Requests": 1,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "DNT": 1,
        "Accept-Encoding": "gzip, deflate, sdch",
        "Accept-Language": "en-US,en;q=0.8",
        #Works for first 3 URLs
        #"Cookie": "SUB=_2AkMgvaKYdcNhrAZWm_AQzmrra4xH-jzEiebBAn7uJhMyOhgv7lshqSVtNqWbRPnk-ZY4u9HgMMMlNScgUQ..;",
        "Cookie": "SUB=_2A2565UmrDeTxGeNJ6FoU8i_FzD2IHXVZkzxjrDV8PUNbmtBeLRejkW9P_EHXHsd8jC_g9V-mXxcWz92Awg..;  SSOLoginState=1474378235; un=anglenet@sina.cn",
        }
    # Above URIs are fine
    #url = "http://weibo.com/u/1000000000"
    ret = requests.get(url, headers = headers)
    ret.encoding="utf8"
    for i in ret.history:
        print(i.headers)
        print(i.status_code)
    print(ret.text)


